package com.yanqu.road.dao.impl.activity.xingyoushanshui.cross;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiTeamMapData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossXingYouShanShuiTeamMapDataDaoImpl extends TempDataObjectDao<CrossXingYouShanShuiTeamMapData> {

    public static String getCrossXingYouShanShuiTeamMapDataSql = "select * from `t_c_xing_you_team_map_data` where `activityId`=? and groupId = ?";
    public Map<Integer, Map<Integer, CrossXingYouShanShuiTeamMapData>> getCrossXingYouShanShuiTeamMapData(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, CrossXingYouShanShuiTeamMapData>> teamMapDataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossXingYouShanShuiTeamMapDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossXingYouShanShuiTeamMapData mapData = parse(rs);
                if(!teamMapDataMap.containsKey(mapData.getTeamId())){
                    teamMapDataMap.put(mapData.getTeamId(), new ConcurrentHashMap<>());
                }
                teamMapDataMap.get(mapData.getTeamId()).put(mapData.getMapIndex(), mapData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return teamMapDataMap;
    }

    public static String updateCrossXingYouShanShuiTeamMapDataSql = "update `t_c_xing_you_team_map_data` set buildLv=?,tzUserId=?,updateTime=?,closeTimes=? where activityId=? and groupId = ? and teamId=? and mapIndex=? and gridId=?";
    @Override
    protected boolean updateData(CrossXingYouShanShuiTeamMapData crossXingYouShanShuiTeamMapData) {
        crossXingYouShanShuiTeamMapData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamMapData.getBuildLv()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiTeamMapData.getTzUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiTeamMapData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamMapData.getCloseTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamMapData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamMapData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamMapData.getTeamId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamMapData.getMapIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamMapData.getGridId()));
        if (getDb().executeNoneQuery(updateCrossXingYouShanShuiTeamMapDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossXingYouShanShuiTeamMapDataSql = "insert into `t_c_xing_you_team_map_data`(activityId,groupId,teamId,mapIndex,gridId,buildLv,tzUserId,updateTime,closeTimes) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossXingYouShanShuiTeamMapData crossXingYouShanShuiTeamMapData) {
        crossXingYouShanShuiTeamMapData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamMapData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamMapData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamMapData.getTeamId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamMapData.getMapIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamMapData.getGridId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamMapData.getBuildLv()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiTeamMapData.getTzUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiTeamMapData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamMapData.getCloseTimes()));
        if (getDb().executeNoneQuery(insertCrossXingYouShanShuiTeamMapDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossXingYouShanShuiTeamMapData parseData(ResultSet rs) throws SQLException {
        CrossXingYouShanShuiTeamMapData crossXingYouShanShuiTeamMapData = new CrossXingYouShanShuiTeamMapData();
        crossXingYouShanShuiTeamMapData.setActivityId(rs.getInt("activityId"));
        crossXingYouShanShuiTeamMapData.setTeamId(rs.getInt("teamId"));
        crossXingYouShanShuiTeamMapData.setMapIndex(rs.getInt("mapIndex"));
        crossXingYouShanShuiTeamMapData.setGridId(rs.getInt("gridId"));
        crossXingYouShanShuiTeamMapData.setBuildLv(rs.getInt("buildLv"));
        crossXingYouShanShuiTeamMapData.setTzUserId(rs.getLong("tzUserId"));
        crossXingYouShanShuiTeamMapData.setUpdateTime(rs.getLong("updateTime"));
        crossXingYouShanShuiTeamMapData.setCloseTimes(rs.getInt("closeTimes"));
        crossXingYouShanShuiTeamMapData.setGroupId(rs.getInt("groupId"));

        return crossXingYouShanShuiTeamMapData;
    }
}
